package com.lemner.motion.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.lemner.motion.R;
import com.lemner.motion.base.BaseActivity;
import com.lemner.motion.base.BaseListener;
import com.lemner.motion.base.BaseModel;
import com.lemner.motion.bean.BaseBean;
import com.lemner.motion.util.FullScreen;

public class ChangePhoneActivity extends BaseActivity {
  private EditText et_phone,et_code;
  private Button btn_code,btn_done;
  private ImageView iv_back;
   private String  phone,code,id;
    @Override
    public int getLayoutFile() {
        return R.layout.activity_change_phone;
    }

    @Override
    public void initView() {
        FullScreen.fullScreen(this,0x0020c26f);
        et_phone=(EditText)findViewById(R.id.et_phone);
        et_code=(EditText)findViewById(R.id.et_code);
        btn_code=(Button)findViewById(R.id.btn_code);
        btn_done=(Button)findViewById(R.id.btn_done);
        iv_back=(ImageView)findViewById(R.id.iv_back);
    }

    @Override
    public void initEvent() {
        iv_back.setOnClickListener(this);
        btn_done.setOnClickListener(this);
        btn_code.setOnClickListener(this);
    }

    @Override
    public void initData() {
        id=getIntent().getStringExtra("id");
    }

    final CountDownTimer countDownTimer=new CountDownTimer(60000,1000) {
        @Override
        public void onTick(long l) {
            btn_code.setClickable(false);
            btn_code.setTextColor(Color.rgb(182, 180, 180));
            btn_code.setText((l / 1000 + "s") + "后重新获取");
            btn_code.setBackgroundResource(R.drawable.circle3);
        }
        @Override
        public void onFinish() {
            btn_code.setText("获取验证码");
            btn_code.setClickable(true);
            btn_code.setTextColor(Color.rgb(255,255,255));
            btn_code.setBackgroundResource(R.drawable.index_circle);
        }
    };

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        Intent intent=new Intent(ChangePhoneActivity.this,ChangeUserActivity.class);
        startActivity(intent);
        finish();
    }

        @Override
    public void onClick(View v) {
       switch (v.getId()){
           case R.id.iv_back:
               startActivity(new Intent(ChangePhoneActivity.this,ChangeUserActivity.class));
               finish();
               break;
           case R.id.btn_code:
             phone=et_phone.getText().toString();
               if(phone.length()!=11)
                   showToast("手机号有误，请检查后再试");
               else {
                   requestCode(phone);
                   countDownTimer.start();
               }
               break;
           case R.id.btn_done:
               code=et_code.getText().toString();
               if(TextUtils.isEmpty(phone)||TextUtils.isEmpty(code))
                   showToast("未能完成输入，检查后再试");
               else
                   changePhone(phone,code);

               break;
       }
    }


    public void requestCode(String phone){
        BaseModel<BaseBean> model=new BaseModel<>();
        retrofit2.Call<BaseBean>call=model.service.getCode(phone);
        model.callEnqueue(call,listener);
    }
    BaseListener<BaseBean> listener=new BaseListener<BaseBean>() {
        @Override
        public void onResponse(BaseBean baseBean) {
            if(baseBean.getErrorCode().equals("0000"))
                forceonline(ChangePhoneActivity.this);
            if(baseBean.getReturnCode().equals("0001"))
                showToast("发送验证码成功");
            else
                showToast("手机号已经使用");
        }

        @Override
        public void onFail(String msg) {
            showToast(msg);
        }
    };

    BaseListener<BaseBean>listener1=new BaseListener<BaseBean>() {
        @Override
        public void onResponse(BaseBean baseBean) {
            if(baseBean.getErrorCode().equals("0000"))
                forceonline(ChangePhoneActivity.this);
            else  if (baseBean.getReturnCode().equals("0001"))
                showToast("修改手机号成功");
            else  if(baseBean.getErrorCode().equals("0008"))
                showToast("手机号已经被使用");
            else
                showToast("未知错误");
        }

        @Override
        public void onFail(String msg) {
           showToast(msg);
        }
    };

    public void changePhone(String phone ,String code){
        BaseModel<BaseBean> model=new BaseModel<>();
        retrofit2.Call<BaseBean>call=model.service.updatephone(id,phone,code,LoginActivity.session);
        model.callEnqueue(call,listener1);
    }



}
